package com.csx.governance.traffic.condition.constant;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author cuisongxu
 * @date 2023/2/25 14:14
 */
public enum CompareOperator {
    /**
     * 等于
     */
    EQ("==", "等于"),

    /**
     * 不等于
     */
    NE("!=", "不等于"),

    /**
     * 大于
     */
    GT(">", "大于"),

    /**
     * 小于
     */
    LT("<", "小于"),

    /**
     * 大于等于
     */
    GE(">=", "大于等于"),

    /**
     * 小于等于
     */
    LE("<=", "小于等于"),

    /**
     * 包含
     */
    INCLUDE("contains", "包含"),

    /**
     * 不包含
     */
    EXCLUDE("contains", "不包含"),

    /**
     * 字符串匹配
     */
    MATCH("match", "字符串匹配");


    private static final Map<String, CompareOperator> OPERATOR_MAP;

    static {
        Map<String, CompareOperator> map = Arrays.stream(CompareOperator.values())
                .filter(operator -> operator.ordinal() <= INCLUDE.ordinal())
                .collect(Collectors.toMap(CompareOperator::symbol, Function.identity()));
//        map.putAll(Arrays.stream(CompareOperator.values()).collect(Collectors.toMap(CompareOperator::symbol, Function.identity())));

        OPERATOR_MAP = map;
    }

    /**
     * 运算符号
     */
    private final String symbol;
    /**
     * 描述
     */
    private final String description;

    CompareOperator(String symbol, String description) {
        this.symbol = symbol;
        this.description = description;
    }

    public final String symbol() {
        return this.symbol;
    }

    public static CompareOperator fromNameOrSymbol(String nameOrSymbol) {
        return OPERATOR_MAP.get(nameOrSymbol.toLowerCase());
    }
}
